home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / update-gconf-defaults < prev    next >
Encoding:
Text File  |  2010-11-06  |  5.4 KB  |  181 lines

  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # ┬⌐ 2005 Josselin Mouette <joss@debian.org>
  5. # Licensed under the GNU LGPL, see /usr/share/common-licenses/LGPL-2.1
  6.  
  7. treefile = '%gconf-tree.xml'
  8.  
  9. import os,tempfile,shutil,sys
  10. from optparse import OptionParser
  11.  
  12. parser = OptionParser()
  13. parser.add_option("--source", dest="source_dir", default="/usr/share/gconf/defaults",
  14.                   help="directory where to find the defaults", metavar="DIR")
  15. parser.add_option("--destination", dest="dest_dir", default="/var/lib/gconf/debian.defaults",
  16.                   help="directory where to build the GConf tree", metavar="DIR")
  17. parser.add_option("--mandatory", action="store_true", default=False, dest="mandatory",
  18.                   help="select mandatory settings directories")
  19. parser.add_option("--no-signal", action="store_false", default=True, dest="signal",
  20.                   help="do not send SIGHUP the running gconfd-2 processes")
  21. parser.add_option("--only-if-changed", action="store_true", default=False, dest="ifchanged",
  22.                   help="only regenerate configuration if needed")
  23.  
  24. (options, args) = parser.parse_args()
  25.  
  26. if 'DPKG_RUNNING_VERSION' in os.environ and options.signal:
  27.     # This is what happens when we are called in an obsolete postinst/prerm script
  28.     # Do nothing, it will be done in the trigger
  29.     sys.exit(0)
  30.  
  31. if options.mandatory:
  32.     options.source_dir="/usr/share/gconf/mandatory"
  33.     options.dest_dir="/var/lib/gconf/debian.mandatory"
  34.  
  35. if not os.path.isdir(options.source_dir):
  36.     parser.error("Source directory does not exist.")
  37. if not os.path.isdir(options.dest_dir):
  38.     parser.error("Destination directory does not exist.")
  39. if not os.access(options.source_dir,os.R_OK|os.X_OK):
  40.     parser.error("Source directory is not readable.")
  41. if not os.access(options.dest_dir,os.W_OK|os.X_OK):
  42.     parser.error("Destination directory is not writable.")
  43.  
  44. save_stdout=os.dup(1)
  45. os.close(1)
  46.  
  47. def cleanup():
  48.   os.dup2(save_stdout,1)
  49.   os.close(save_stdout)
  50.   shutil.rmtree(tmp_dir)
  51.  
  52. def htmlescape(str):
  53.   return str.replace('&','&').replace('>','>').replace('<','<').replace('"','"')
  54.  
  55. def int_entry(value):
  56.   return '  <int>' + value + '</int>\n'
  57.  
  58. def bool_entry(value):
  59.   return '  <bool>' + value + '</bool>\n'
  60.  
  61. def float_entry(value):
  62.   return '  <float>' + value + '</float>\n'
  63.  
  64. def string_entry(value):
  65.   return '  <string>' + htmlescape(value) + '</string>\n'
  66.  
  67. def list_entry(value):
  68.   ret = '  <list type="string">\n'
  69.   for v in value[1:-1].split(','):
  70.     ret += '    <value><string>' + htmlescape(v) + '</string></value>\n'
  71.   ret += '  </list>\n'
  72.   return ret
  73.  
  74.  
  75. def listcmp(a,b):
  76.   """Number of starting similar elements in a and b"""
  77.   m = min(len(a),len(b))
  78.   for i in range(m):
  79.     if a[i] != b[i]:
  80.       return i
  81.   return m
  82.  
  83. def apply_entries(filename):
  84.   res=os.spawnvpe(os.P_WAIT,'gconftool-2',
  85.            ['gconftool-2','--direct','--config-source',
  86.             'xml:merged:'+tmp_gconf,'--load',filename],
  87.            {'HOME': tmp_home})
  88.   if res:
  89.     cleanup()
  90.     sys.exit(res)
  91.  
  92. gconf_val = {}
  93.  
  94. def write_and_apply_entries(filename):
  95.   out=file(filename,'w')
  96.   out.write('<gconfentryfile>\n<entrylist base="/">\n')
  97.   for key in gconf_val:
  98.     out.write('<entry>\n<key>' + key + '</key>\n<value>\n')
  99.     # write the current entry
  100.     value = gconf_val[key]
  101.     if value[0] == '"' and value[-1] == '"':
  102.       out.write(string_entry(value[1:-1]))
  103.     elif value in ['true','false']:
  104.       out.write(bool_entry(value))
  105.     elif value[0] == '[' and value[-1] == ']':
  106.       out.write(list_entry(value))
  107.     elif value.isdigit():
  108.       out.write(int_entry(value))
  109.     else:
  110.       try:
  111.         float(value)
  112.         out.write(float_entry(value))
  113.       except ValueError:
  114.         out.write(string_entry(value))
  115.     out.write('</value>\n</entry>\n')
  116.   out.write('</entrylist>\n</gconfentryfile>\n')
  117.   out.close()
  118.   apply_entries(filename)
  119.  
  120. def read_entries(filename):
  121.   for line in file(filename):
  122.     l = line.rstrip('\n').split(None,1)
  123.     if len(l) == 2 and not l[0].startswith('#'):
  124.       gconf_val[l[0]] = l[1]
  125.  
  126.  
  127. defaults_files = []
  128. for f in os.listdir(options.source_dir):
  129.   for ext in ['.dpkg-tmp', '.bak', '.tmp', '~', '.sav', '.save']:
  130.     if f.endswith(ext):
  131.       break
  132.   else:
  133.     if os.path.exists(os.path.join(options.source_dir, f)):
  134.       defaults_files.append(f)
  135. defaults_files.sort()
  136.  
  137. if options.ifchanged:
  138.   source_stamp = os.stat(options.source_dir).st_mtime
  139.   for f in defaults_files:
  140.     realname=os.path.join(options.source_dir,f)
  141.     source_stamp = max(os.stat(realname).st_mtime,source_stamp)
  142.   try:
  143.     dest_stamp = os.stat(os.path.join(options.dest_dir, treefile)).st_mtime
  144.   except OSError:
  145.     dest_stamp = 0
  146.   if source_stamp < dest_stamp:
  147.     sys.exit(0)
  148.  
  149. tmp_dir=tempfile.mkdtemp(prefix="gconf-")
  150. tmp_home=tmp_dir+'/home'
  151. tmp_gconf=tmp_dir+'/gconf'
  152. tmp_file=tmp_dir+'/temp.entries'
  153.  
  154. for f in defaults_files:
  155.   realname=os.path.join(options.source_dir,f)
  156.   if f.endswith('.entries'):
  157.     if gconf_val:
  158.       write_and_apply_entries(tmp_file)
  159.       gconf_val={}
  160.     apply_entries(realname)
  161.   else:
  162.     read_entries(realname)
  163. if gconf_val:
  164.   write_and_apply_entries(tmp_file)
  165.  
  166. try:
  167.   shutil.copyfile(tmp_gconf+'/'+treefile,options.dest_dir+'/'+treefile+'.tmp')
  168.   os.rename(options.dest_dir+'/'+treefile+'.tmp',options.dest_dir+'/'+treefile)
  169. except IOError:
  170.   # No %gconf-tree.xml file was created.
  171.   try:
  172.     os.remove(options.dest_dir+'/'+treefile)
  173.   except OSError:
  174.     # No existing file
  175.     pass
  176.  
  177. cleanup()
  178.  
  179. if options.signal:
  180.     os.system('kill -s HUP `pidof gconfd-2` >/dev/null 2>&1')
  181.